<?php

namespace  App\Http\Controllers\V1;

use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Http\Controllers\Controller;
use App\Models\Factory\CollectionFactory;
use App\Models\Factory\ProductFactory;
use App\Models\Factory\UserFactory;
use App\Models\Factory\V0\DeliveryFactory;
use App\Models\Orm\DataProductApply;
use App\Models\Orm\Product;
use App\Models\Orm\Recommand;
use Illuminate\Http\Request;

class ProductController extends  Controller
{
    public function getProduct(Request $request)
    {
        $info = $request ->all();
        $data = ProductFactory::getList($info['type']);

        return RestResponseFactory::ok($data,200);

    }


    public function apply(Request $request)
    {
        $info = $request ->all();
        $user = $request -> get('user');


        $data = ProductFactory::addProductApply($user,$info);

        if($data == false){
            SLogger::getStream()->info($user->id.'点击统计插入失败');
        }

        return RestResponseFactory::ok(RestUtils::getStdObj(),200);

    }


    public function detail(Request $request)
    {
        $info = $request ->all();
        $user = $request -> get('user');


        $data = ProductFactory::addProductDetail($user,$info);

        if($data == false){
            SLogger::getStream()->info($user->id.'详情统计插入失败');
        }

        return RestResponseFactory::ok(RestUtils::getStdObj(),200);

    }

    public function Screening(Request $request)
    {

        //接受参数
        $data = $request->input();


        $amount = intval($data['amount']);

        //查询
        $result = ProductFactory::screening( $amount);


        return RestResponseFactory::ok($result);

    }

    //获取连接
    public function getUrl(Request $request)
    {

        //接受参数
        $data = $request->input();

        //查询
        $result = ProductFactory::getUrl($data['name']);


        return RestResponseFactory::ok($result);

    }

    //贷友推荐
    public function getRecommand(Request $request)
    {
        $data = ProductFactory::getRecommand();
        return RestResponseFactory::ok($data);

    }
    //贷友推荐
    public function getVip(Request $request)
    {
        $data = ProductFactory::getVip();
        return RestResponseFactory::ok($data);

    }


    public function tongji(Request $request)
    {

        //接受参数
        $data = $request->input();

        $channels = DeliveryFactory::getDeliveryByNid($data['channel']);

        $data['channel'] =  $channels ? $channels->id : 1;

        $product = Product::where('id',$data['product_id'])->first();
        $data['product_name'] = $product?$product->name :'产品';
        $data['create_ip'] = Utils::ipAddress();
        $data['app_name'] = '998';
        $data['product_id'] = $product?$product->id :'1';
        $data['create_at'] = date('Y-m-d H:i:s',time());
        $result = DataProductApply::create($data);

       return redirect($product->link);

    }



    //新品
    public function new(Request $request)
    {
        $data = ProductFactory::getVip();

        return RestResponseFactory::ok($data);


    }


    }
